package com.example.administrator.rxjavarxandroid_retrofit.http.filter;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by chenboge on 16/9/11.
 */

/**
 * restful
 * 此interceptor用于在每次发起请求前对请求header进行处理，添加需要字段到header中，或修改header中的参数值
 */
public class AuthInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request origin = chain.request();
        //原始请求header
        Headers originHeaders = origin.headers();

        //创建需要的请求header
        Headers.Builder newHeaders = new Headers.Builder();

        //向请求头写入Content-Type参数，设置参数搁置为application/json
        newHeaders.add("Content-Type", "application/json;charset=UTF-8");

        //String authType = "Cookie";
        for (int i = 0, size = originHeaders.size(); i < size; i++) {
           // if (!TextUtils.equals(originHeaders.name(i), "Auth-Type")) {        // 2
            newHeaders.add(originHeaders.name(i), originHeaders.value(i));
           // } else {
                //authType = originHeaders.value(i);
           // }
        }
//找到header中的cookie字段，写入cookie值,也可以自定义一些键值对
       // newHeaders.add("Cookie", "");
//        switch (authType) {                                                     // 3
//            case "Basic":
//                //基本请求不带cookie参数
//                //basicAuth(newRequest, origin.url(), System.currentTimeMillis());
//                break;
//            default:
//                newHeaders.add("Cookie", "");
//                break;
//        }
        Request.Builder newRequest = origin.newBuilder()
                .headers(newHeaders.build());
        return chain.proceed(newRequest.build());
    }


}
